//standard

if(window.CH == undefined){
    window.CH = {};
}


CH.VC2={
    packagename:"standard",
    undoflag:0,
    imagesuploaded:0,
    sDiv:"",
    xangle:0,
    counter:4,
    totalcount:4,
    tempdivid:"",
    tempspanid:"",
    items:{},
    tosave:"",
    angle:0,
    selecteditem:0,
    imageortext:0,
    imgparam:null,
    filename:"",
    jcrop_api:0,
    boundx:0,
    boundy:0,
    copied:null,
    lastaction:"",
    prevstate:"",
    element:"",
    lastalignstate:"Ralign",
    lastfontfam:"serif",
    lastfontsize:"",
    lastcolor:"",
    lasttext:"",
    loaded: false,
    appended: false,
    DivBeingRotated:false,  
    mouseStartAngle:0,    
    DivStartAngle:false  ,
    dirname:"bgimgs/",
    spanvalue:"Company Adress",
    
    backmade:0,
    backhtml:"",
    frontmade:0,
    fronthtml:"",
    temp:1,
    
    
    
    
    init:function(){
        CH.VC3.uninitall();
        CH.VC2.uninitall();
        CH.VC.uninitall();


        this.initSelection();
        this.initunselect();
        this.initBTchangeBG();
        this.initeditinplace();    
        this.init_pre_backgroud();    
        this.to_back();
        this.to_front();

        this.initchoose_design();
        this.initadd_text();
        this.initadd_photo();
        makeback();
        matchaddressfromback();
        
    // this.Setprebackground();


    },
    uninitall:function(){
        $(".back").remove();
        // alert("calling dead");
        $(".probtn").unbind("click");
        $(".txtbtn").unbind("click");
        $(".uplodtbtn").unbind("click");
        $('#backdivimg').unbind("click");
        $('#frontdivimg').unbind("click");
        $("#predefinebg").unbind("click")
        $('span').unbind("dblclick");
        $(".drop div").unbind("mousedown");
        $(".drop").unbind("click");
        $("#changebg").unbind("click");
        
   
    },
    
    
    initprebackgroundsrc:function(){  //redundant
        
        if(temp==1){
            $('#tempo').append("<div align='center'><h1><font color='white'>Choose a package</font></h1></div><div><div style='float:left;'><img id='standardpkg' class='package' width='150px' height='130px' src='imagesapp/standard.png' /></br><font size='1' color='white'>Standard Option</br> In this Option you can:</br>1. Choose a Background Image.</br> 2. Choose and Edit Address to be &nbsp&nbsp&nbsp</br> printed on the Advent Kalender.</br><button class='buttons' id='standardButton'>standard package </button></div><div style='float:left;'>--</br>--</br>--</br>--</div><div style='float:left;'><img id='basicpkg' class='package' width='150px' height='130px' src='imagesapp/basic.png' /></br>Basic Option</br> In this Option you can:</br>1. Choose a Background Image.</br> 2. Choose and Edit Address to be</br> printed on the Advent Kalender.</br>3. Place text and photos with in </br>the White Areas of the background </br>images.</br><button class='buttons' id='basicButton'>basic package </button></div><div style='float:left;'>--</br>--</br>--</br>--</div><div style='float:left;'><img id='businesspkg' class='package' width='150px' height='130px' src='imagesapp/business.png' /></br>Standard Option</br> In this Option you can:</br>1. Choose the shape of the Advent Kalender.</br> 2. Choose the fillings of the Advent Kalender.</br>3. Choose Address to be printed on the back.</br> 4. On the backside the user can only change</br>color and address.</br>5. On the front the user can do any thing,</br> 6. On the side, the user can just change colors.</br><button class='buttons' id='businessButton'>business package </button></div></div></br></font>")
            $('#tempo').stop().animate({
                'width':'520px'
            }, 520);
            temp=0;

            $('#standardButton').click(function(){
                //CH.VC.init();// I have to make a new init for this
                $(".drop").html("");
                $( ".tools button" ).prop("disabled","disabled");
                $( ".tools #changebg" ).prop("disabled","");
                $( ".tools #predefinebg" ).prop("disabled","");
                $( ".tools input" ).prop("disabled","disabled");
                $( ".tools select" ).prop("disabled","disabled");
                                
                            
                            
                CH.VC2.init();
                         
                $(this).prop("id","");
                $('#tempo')
                .html(" ")
                .stop().animate({
                    'width':'0px'
                }, 520);
                temp=1;   
            });
                        

        }
        else
        {
            $(this).prop("id","");
            $('#tempo')
                    
            .html(" ")
            .stop().animate({
                'width':'0px'
            }, 520);
            temp=1;   
        }
    

              
        
        
    },
    
    initchoose_design:function() {
        
        var oThis=this;
        $(".probtn").click(function(){

            alert("You cannot choose design in standard package please upgrade to business package");

        });
    },
    initadd_text:function() {
        
        var oThis=this;
        $(".txtbtn").click(function(){  
            alert("You cannot add text in standard package please upgrade to business package");
    
        });
    },


        
    initadd_photo:function() {
        
        var oThis=this;
        $(".uplodtbtn").click(function(){
            alert("You cannot upload picture in standard package please upgrade to business package");
        

        });
    },
    //<div class="drop" id="drop"></div>
    
    to_back:function() {
        
        var oThis=this;
        $("#backdivimg").click(function(){
            $('#backdivimg').hide();
            $('#frontdivimg').show();
            cmn_to_back(CH.VC2.packagename);
    
        });
    },
    
    
    
    to_front:function(){
        var oThis=this;
       
        $("#frontdivimg").click(function(){
           
            cmn_to_front(CH.VC2.packagename);
        });
    
    },
    
        
    init_pre_backgroud:function() {
        
        var oThis=this;
        $("#predefinebg").click(function(){
            //oThis.Setprebackground();
            //alert("sdf");
            $("#addressdiv").hide();
            $("#leftsidetable td").html("choose a background");
            CH.VC2.putBackGroundIndd();
        });
    },
    
    Setprebackground:function(){
        $.ajax({
                        
            type: "POST",
            url:"showbgimg.php",
            data:{
                src:CH.VC2.packagename
            } ,
            //success:function(data){
            success:function(data){
                                
                // alert(data);
                $("#display")
                .empty()
                .html(data);
                           
                $("#display").dialog("destroy");
                $("#display").dialog({
                    modal: true,
                    width:"auto",
                    height:"auto",
                    open:function(event, ui){
                    
                        $("#display img").click(function(e) {
                            
                            //alert($(this).prop("src"));
                            var bg=$(this).prop("src");
                            $.ajax({
                        
                                type: "POST",
                                url:"CopyImgFrm.php",
                                data:{
                                    bgsrc:bg
                                },
                                //success:function(data){
                                success:function(data){
                            
                                    //alert("hello");
                                    //alert(data);
                                    $('.drop').css('background-image', 'url()');
                                    $('.drop').hide();
                            
                                    $('.drop').show();
                                    //alert("ahahah");
                                    //alert($('.drop').css('background-image'));
                                    var url = "url(./imagesapp/back.png?"+Math.random()+")";
                                    $('.drop').css('background-image', url);
                                    $("#display").dialog( "close" );
                                } 

                            })
                            
                
                           
                        });
                        
                    },
                    buttons: {
                        
                        
            
                        Close: function() {
                            $( this ).dialog( "close" );
                        }
            
                                   
                        
                    }
                });
            }
        });
        
    },
    
    
    initeditinplace:function() {
        var oThis=this;
        $('span').live('dblclick', function() {
            // if success message is visible, remove it
            $(this).find('.ok').remove();
        
            // if already in input mode, return
            if ($(this).hasClass('inputMode')) {
                return true;
            }
        
            // change to input mode
            var tartalom = $(this).html();
        
        
            $(this)
            .empty()
            
            .append("<input type='text' id='input2' style='background-color:transparent; font-size:30px;' value='" + tartalom + "'>")
            // style='float:left;'
            //.append("<input type='button' style='float:left;' value='S' class='save'>")
            .addClass('inputMode');
            
            
            $("#input2").unbind("focusout");
            $("#input2").focusout(function() {
                
                //alert($(this).parent().prop("id")); span ki id
                $(this).parent().html($(this).val())
        
        
                //.html($(this).prev().val())
                //.append(jSuccess)
                .removeClass('inputMode');
        
        
                CH.VC2.spanvalue=$(this).val();
                oThis.to_back();
                // alert(CH.VC2.spanvalue);
                // animate success message
                //  $(jSuccess).delay(1500).fadeOut(500, function() {
                //      $(this).remove();
                // });
        
                // prevent jq_edit click handler
                return false;
            });
        });
    },
    
    
    

    initSelection:function(){
        var oThis=this;
        $(".drop div").mousedown(function(){
            var seldiv="#"+$(this).prop("id")
            oThis.selectelement(seldiv);
        
        });
        
        
    },
    
    selectelement:function(proDivId){
        
        $(".drop div").removeClass("highlight");
            
        // window.console.log("select");
        $(".drop div .rotate-image").hide();
        $(".drop div .delete-image").hide();
        $(".drop div .drag-image").hide();
        //alert("selection "+$(this).prop("id"));
        //this.sDiv="#"+$(this).prop("id");
            
        this.sDiv=("#"+($(proDivId).prop("id")));
        //window.console.log(this.sDiv);
        $(this.sDiv).addClass("highlight");
        $(this.sDiv+" .rotate-image").show();
        $(this.sDiv+" .drag-image").show();
        $(this.sDiv+" .delete-image").show();
        
        this.selecteditem =this.sDiv.substr(this.sDiv.length - 1);
        this.selecteditem--;
            
            
    //window.console.log("In Select sDiv: "+this.sDiv);
    //this.initDrag();
    },
    
    
    

    initunselect:function(){
        var oThis=this;
        $(".drop").unbind("click");
        $(".drop").click(function(e) {
            //$(".drop").click(function(e) {
            oThis.unselectelement(e);       
                
        });
    
    },
    
    unselectelement:function(e){
        
        if($(e.target).prop("id")=="drop")
        {
            $(".drop div").removeClass("highlight");
            $(".drop div .rotate-image").hide();
            $(".drop div .delete-image").hide();
            $(".drop div .drag-image").hide();
            this.sDiv="";
            return true;
        }
           
        return false;
    },



    //==========================================================================================
    
    //==========================================================================================
   
    
   
	
    
    
    
    initBTchangeBG:function(){  //change background
        var oThis=this;
        $("#changebg").unbind("click");
        $("#changebg").click(function() {
            
            
            CH.VC2.changebackground();    
        });
        
    },
    
    
    
    changebackground:function(){ 
    
    
        $("#imageform2").replaceWith("");
    
        var formStr = " <form id='imageform2' method='post' style='display:none;' enctype='multipart/form-data' action='./ajaximage2.php'>"+
        "Upload your image <input type='file' name='photoimg2' id='photoimg2' />"+
        "<input type='hidden' name='rand1' value='"+Math.random()+"' />" +
        "</form>";
    
        $("#outer").html("");
        $("#outer").html(formStr);
        //$("#photoimg2").replaceWith("<input type='file' name='photoimg2' id='photoimg2' />"+Math.random());
    
    
        $("#imageform2").dialog("destroy");
        $("#imageform2").dialog({
            modal: true,
            open:function(event, ui){
            //$("#imagefrm").show();
                
            },
            buttons: {
                Ok: function() {
                    var temp=($("#imageform2 #photoimg2").val());
                    var result = temp.substring(temp.lastIndexOf("."));
                    
                    //alert(result);
                    if(result==".jpg"||result==".png")
                    {
                        $("#imageform2").ajaxForm(
                        {

                            success:    function(responseText, statusText, xhr, $form) { 
                           
                           
                                CH.VC.fitbackground();
                           
                                //ajax call here
                          
                           
                                //alert("here Ia"+responseText);
                                $('.drop').css('background-image', 'url()');
                                $('.drop').hide();
                            
                                $('.drop').show();
                                //alert("ahahah");
                                //alert($('.drop').css('background-image'));
                                var url = "url(./imagesapp/back.png?"+Math.random()+")";
                                $('.drop').css('background-image', url);
                                $('.back').css('background-image', url);
                            
                            
                            
                            
                            

                            } 
                
                        }).submit();
                        $( this ).dialog( "close" );
                    }
                    else{
                        $( this ).dialog( "close" );
                        alert("Please Select jpg or png image for background");
                        CH.VC2.changebackground();
                    }
                        
                        
                }
            }
        });
    },
    
    
    
    /*changebackground:function(){ 
    
    //alert("adsf");
        $(".outer").append(" <form id='imageform2' method='post' style='display:none;' enctype='multipart/form-data' action='./ajaximage2.php'>Upload your image <input type='file' name='photoimg2' id='photoimg2' /></form>");
    
        $("#imageform2").dialog({
            modal: true,
            open:function(event, ui){
            //$("#imagefrm").show();
                
            },
            buttons: {
                Ok: function() {
                    $("#imageform2").ajaxForm(
                    {

                        success:    function(responseText, statusText, xhr, $form) { 
                            //alert($('.drop').css('background-image'));
                            $('.drop').css('background-image', 'url()');
                            $('.drop').hide();
                            
                            $('.drop').show();
                            //alert("ahahah");
                            //alert($('.drop').css('background-image'));
                            var url = "url(./imagesapp/back.png?"+Math.random()+")";
                            $('.drop').css('background-image', url);
                        //alert($('.drop').css('background-image'));

                        } 

                    }).submit();
                    $( this ).dialog( "close" );
                }
            }
        });
    
  
        
   
    
    },
    
    
    */
    putBackGroundInddforinitialscreen:function(bannertemphtml) 
    {
        _packageType = 'STANDARD';
        loadDesigns(CH.VC2.packagename, false);			
    }    
    
}
